package com.slmxz.tms.common.component;

import com.slmxz.tms.common.constant.SecurityConstants;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.common.util.RequestUtil;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.security.util.JwtUtils;
import com.slmxz.tms.system.model.vo.UserInfoVO;

public class RedisUserService {
    /**
     * @return 从缓存中取出登录用户
     */
    public static UserInfoVO currentUser() {
        //从请求头中获取授权 token
        String authToken = RequestUtil.getAuthToken();
        //还没有登录
        if(authToken == null || !authToken.startsWith(SecurityConstants.JWT_TOKEN_PREFIX)){
            return null;
        }
        //去掉token中的干扰内容
        String token = authToken.substring(SecurityConstants.JWT_TOKEN_PREFIX.length());// The part after "Bearer "
        //用token换取登录的账户
        String account = JwtUtils.parseToken(token);
        //当注销用户时，token还存在，但是账户不存在
        return RedisService.getUser(account);
    }

}
